﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using xKnight.Models;
using xKnight.WebCrawling.Core.Authentication;
using xKnight.WebCrawling.Event;
using xKnight.WebCrawling.Models;

namespace xKnight.WebCrawling
{
    class Program
    {
        static void Main(string[] args)
        {

            WebCrawlingSetting setting = new WebCrawlingSetting()
                                       {
                                           MaxDepth =10,
                                           MaxByte=null,
                                           MaxPages = null,
                                           MaxTime = null
                                       };

            
            AuthenticationSetting authSetting = new AuthenticationSetting()
            {
                LoginFormAction = "http://www.tebyan.net/newindex.aspx?pid=7&amp;retUrl=",
                LoginFormMethod="post",
                LoginPageUrl = "http://www.tebyan.net/newindex.aspx?pid=7&retUrl=",
                PasswordField = "ctl07$ctl00$PasswordText",
                PasswordValue="123456789",
                UsernameField = "ctl07$ctl00$UsernameText",
                UsernameValue="ehsanzerang"
            };
             

            /*
            AuthenticationSetting authSetting = new AuthenticationSetting()
            {
                LoginFormAction = "https://login.yahoo.com/config/login?",
                LoginFormMethod = "post",
                LoginPageUrl = "https://login.yahoo.com/config/login_verify2?&.src=ym&.intl=us",
                PasswordField = "passwd",
                PasswordValue = "mirmirmir",
                UsernameField = "login",
                UsernameValue = "ehsan_mir2000"
            };*/

            WebCrawlingTarget target = new WebCrawlingTarget("http://www.tebyan.net/", authSetting, 10);

            Crawler crawler = new Crawler(setting,new WebCrawlingTarget[] {target});
            crawler.CrawlAnnounced += crawler_CrawlAnnounced;
            crawler.CrawlCompleted += crawler_CrawlCompleted;
            crawler.CrawlStarted += crawler_CrawlStarted;

            crawler.Crawl();

            Console.ReadLine();
        }

        static void crawler_CrawlStarted(object sender, Event.CrawlStartedEventArgs e)
        {
            DateTime dtStart = e.StartDateTime;
            int crawlId = e.CrawlSettingId;

            Console.WriteLine("Crawling Operation Started");
        }

        static void crawler_CrawlCompleted(object sender, Event.CrawlCompletedEventArgs e)
        {
            DateTime dtFinish = e.FinishDateTime;
            int crawlId = e.CrawlSettingId;
            Console.WriteLine("Crawling Operation Finished");
        }

        static void crawler_CrawlAnnounced(object sender, Event.CrawlAnnouncedEventArgs e)
        {
            CrawlAnnounceItem announceItem = e.CrawlAnnounceItem;
            DateTime dateTime= announceItem.DateTime;
            string description=announceItem.Description;
            
            int totalForms=announceItem.TotalFormsFound;
            int totalUniqueForms= announceItem.TotalUniqueFormsFound;
            
            int totalLinks=announceItem.TotalLinksFound;
            int totalUniqueLinks=announceItem.TotalUniqueLinksFound;

            int totalPagesDownloaded=announceItem.TotalPagesDownloaded;

            Console.WriteLine("Crawling Operation Announced");
        }
    }
}
